package com.jieshen.lesson2;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 入门的事件监听：点击按钮输出一句话
 */
public class ActionEventTest {
    public static void main(String[] args) {

        //按下按钮，触发一些事件
        Frame frame = new Frame();
        Button button = new Button();

        //因为,addActionListener()需要一个ActionListener，所以我们需要构造一个ActionListener
        MyActionListner myActionListner = new MyActionListner();
        button.addActionListener(myActionListner);

        frame.add(button,BorderLayout.CENTER);
        frame.pack();
        windowClose(frame);
        frame.setVisible(true);
    }

    //关闭窗口的方法
    private static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

class MyActionListner implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("aaa");
    }
}
